<?php

namespace app\merchant\controller;

use app\merchant\model\store\Shop as ShopModel;
use app\merchant\model\Region as RegionModel;

/**
 * 门店管理
 * Class Shop
 * @package app\merchant\controller\store
 */
class Shop extends Base
{
    /**
     * 门店列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new ShopModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess($list,"查询成功");
    }


    /**
     * 添加门店
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new ShopModel;
        // 验证能否添加
        if (!$model->validateShop()) {
            return $this->renderError($model->getError() ?: '添加失败');
        }

        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }

    /**
     * 编辑门店
     * @param $shop_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        if (!isset($param['shop_id']) || isZeroNumeric($param['shop_id'])) {
            return $this->renderError([],'参数失败');
        }

        // 门店详情
        $model = ShopModel::detail($param['shop_id']);
        if (!$model) {
            return $this->renderError([],'门店不存在，请刷新重试');
        }

        // 编辑记录
        if ($model->edit($param)) {

            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '编辑失败');
    }

    /**
     * 腾讯地图坐标选取器
     * @return mixed
     */
    public function getpoint()
    {
        $this->view->engine->layout(false);
        return $this->fetch('getpoint');
    }



    /**
     * 删除门店
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if (!isset($param['shop_id']) || isZeroNumeric($param['shop_id'])) {
            return $this->renderError([],'参数失败');
        }

        // 门店详情
        $model = ShopModel::detail($param['shop_id']);
        if (!$model) {
            return $this->renderError([],'门店不存在，请刷新重试');
        }

        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**
     * 门店详情
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if (!isset($param['shop_id']) || isZeroNumeric($param['shop_id'])) {
            return $this->renderError([],'参数失败');
        }
        // 门店详情
        $model = ShopModel::detail($param['shop_id']);
        if (!$model) {
            return $this->renderError([],'门店不存在，请刷新重试');
        }

        return $this->renderSuccess($model,'删除成功');
    }


    /**
     * 获取省份
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function provinceList()
    {
        $model = new RegionModel;
        $data = $model->provinceList();
        return $this->renderSuccess($data,'查询成功');
    }



    /**
     * 获取城市和区域列表
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function otherList()
    {
        $param = $this->postData();
        $id = isset($param['id']) ? trim($param['id']) : 0;

        $model = new RegionModel;
        $data = $model->otherList($id);
        return $this->renderSuccess($data,'查询成功');
    }


}